define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'tasks/task/index',
                    review_url: 'tasks/task/review',
                    table: 'finance_extract_task',
                }
            });
            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'task.id',
                searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                      	{field: 'week.week', title: "周期",formatter: Controller.api.formatter.weekname},
                        {field: 'week.status_text', title: "周状态",},
                        {
                        	field: 'name', title: __('Name'),
                        	table: table,
                            events: Table.api.events.operate,
							buttons: [],
                            formatter: Controller.api.formatter.zdyname

                    	},
                        {field: 'admin.username', title:  __("Taskname")},
                        {field: 'type_text', title: __("Task.type")},
                        {field: 'createtime', title: __("Createtime"), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'week.department_text', title: __("Department")},
                     
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        review: function () {
            Controller.api.bindevent();
            $(document).on('keyup change focus blur', "#review_desc2", function () {
               $(".layui-layer-content").children('iframe').contents().find("#review_desc").val($(this).val());
            });
            $(document).on('click', ".check1", function () {
                $(".layui-layer-content").children('iframe').contents().find("#review_check").val(1);
                //$(".layui-layer-content").children('iframe').contents().find("#add-form").submit();
                $(".okbutton").click();
                
            });   
            $(document).on('click', ".check2", function () {
                $(".layui-layer-content").children('iframe').contents().find("#review_check").val(2);
                //$(".layui-layer-content").children('iframe').contents().find("#add-form").submit();
                $(".okbutton").click();
               
            }); 
            $(document).on('click', ".btn-add-season", function () {
                  var that = this;
                  var extract_week = $("#extract_week").val();
                  Fast.api.open('finance/extract/week/public_add/id/'+extract_week+'/type/Season', '添加新的场次', $(that).data() || {});
            });
           //监听文本框改变事件
            $(document).on('focus', ".fieldlist input", function () {
                var tmptxt = $(this).val();    
                if(tmptxt == 0){
                    $(this).val('');
                }else{
                    $(this).val(tmptxt.replace(/[^\- \d.]/g,''));
                }
            });
            //监听文本框改变事件
            $(document).on('blur', ".fieldlist input", function () {
                var tmptxt = $(this).val();   
                var tepclass = $(this).attr("class");
                if(tmptxt == ''){
                    $(this).val(0);
                }else{
                    $(this).val(tmptxt.replace(/[^\- \d.]/g,''));
                   
                }
            });
            $(document).on('keyup change focus blur', ".type2name", function () {
                var type2num = 0;
                $(".type2name").each(function () {
                    if($(this).val()){
                        type2num +=  parseFloat($(this).val());
                    }
                }); 
                if(type2num && type2num > 100){
                    layer.msg('百分比不能超过100,请修改');
                    $(this).val('');
                }
            });
           // 初始化表格参数配置
            Table.api.init({
                extend: {
                    review_url: 'tasks/task/review',
                    check_url: 'tasks/task/check',
                    table2: 'finance_extract_task',
                }
            });
            var table2 = $("#table2");
            // 初始化表格
            table2.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.review_url,
                pk: 'id',
                sortName: 'task.id',
                searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                       
                        {field: 'week.week', title: "周期",formatter: Controller.api.formatter.weekname},
                        {field: 'week.status_text', title: "周状态"},
                        {field: 'name', title: __('Name')},
                         {
                            field: 'name', title: __('School.School_id'),
                            table: table2,
                            events: Table.api.events.operate,
                            buttons: [],
                            formatter: Controller.api.formatter.SchoolName

                        },
                        {field: 'admin.username', title:  __("Taskname")},
                        {field: 'type_text', title: __("Task.type")},
                         {
                            field: 'name', title: __('Task.Status'),
                            table: table2,
                            events: Table.api.events.operate,
                            buttons: [],
                            formatter: Controller.api.formatter.StatusDesc

                        },
                         
                        {field: 'createtime', title: __("Createtime"), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'week.department_text', title: __("Department")},
                        {field: 'operate', title: __('Operate'), table: table2, events: Table.api.events.operate,buttons: [],formatter:Controller.api.formatter.operate}
                     
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table2);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter:{
               weekname: function (value, row, index) {
				
                    return "第"+row.week.week+"周";
                },
                zdyname: function (value, row, index) {
                    var table = this.table;
                    // 默认按钮组
                    var buttons = [];
                    if(row.type == 1){
                        var url = "integral/outtasks/index";
                    }else{
                        var url = "integral/taskbook/index";
                    }
                    buttons.push({
                                name: 'detail',
                                title: row.name,
                                text: row.name,
                                classname: 'btn btn-xs btn-dialog',
                                url: url,
                                extend:'data-toggle="tooltip" data-area=\'["80%","80%"]\'',
                            });
                    return Table.api.buttonlink(this, buttons, value, row, index, 'buttons');
                },   
                StatusDesc: function (value, row, index) {
                    if(row.type == 1){
                        var status_text =  row.taskotreach.status_text;
                        var status_desc =  row.taskotreach.status_desc;
                        var id =  row.taskotreach.id;
                    }else{
                        var status_text =  row.taskbook.status_text;
                        var status_desc =  row.taskbook.status_desc;
                        var id =  row.taskbook.id;
                    }
                  $(document).off("click", ".btn-ip-"+row.id+id);
                  $(document).on("click", ".btn-ip-"+row.id+id, function () {
                      var content_data ='<table  class="table table-striped table-bordered table-hover table-nowrap" data-operate-edit="1" data-operate-del="1" width="100%"><thead><tr> <th style="text-align: center;">'+__('Task.Date')+'</th><th style="text-align: center;">'+__('Task.Status')+'</th><th style="text-align: center;">'+__('Task.Check')+'</th><th style="text-align: center;">'+__('Task.Publish')+'</th><th style="text-align: center;">'+__('Status_desc')+'</th></tr></thead><tbody id="ultable" >';
                             content_data += status_desc+'</tbody></table>';
                             layer.open({
                               type: 1,
                               skin: 'layui-layer-demo', //样式类名
                               title:'审核记录',
                               //closeBtn: 0, //不显示关闭按钮
                               anim: 2,
                               shadeClose: true, //开启遮罩关闭
                               area: ['50%', '50%'],
                               
                               content: content_data
                            });
                    });
                   return '<a class="btn btn-xs  btn-ip-'+row.id+id+'" > '+status_text+ '</a>';
                },  
                 SchoolName: function (value, row, index) {
                    if(row.type == 1){
                        return row.taskotreach.school_text;
                    }else{
                        return row.taskbook.school_text;
                    }
					
                },
                 operate: function (value, row, index) {
                    var table = this.table;
                    // 操作配置
                    var options = table ? table.bootstrapTable('getOptions') : {};
                    var sstatus = 0;
                    var id = 0;
                    // 默认按钮组
                    var buttons = $.extend([], this.buttons || []);
                    if(row.type == 1){
                        sstatus = row.taskotreach.status;
                        id = row.taskotreach.id;
                    }else{
                        sstatus = row.taskbook.status;
                        id = row.taskbook.id;
                    }
                    var review = 0;
                    switch (true) {
                        case sstatus == 3:
                            review = 1;
                            break;    
                        case sstatus == 4:
                            review = 2;
                            break;
                        case sstatus == 5 || sstatus == 8:
                            review = 3; 
                            break;
                        case sstatus == 6:
                            review = 4;
                            break;
                        case sstatus == 7:
                            review = 5;
                            break;
                        default:
                           break;
                    }
                 
                    if(review){
                         buttons.push({
                            name: 'review'+review,
                            icon: 'fa fa-sticky-note',
                            title: __('Task.Status '+sstatus),
                            classname: 'btn btn-xs btn-warning btn-dialog',
                            extend: 'data-toggle="tooltip" data-area=\'["80%","100%"]\'',
                            url: options.extend.review_url+'/type/'+row.type+'/id/'+id+'/status/'+review
                          });
                    }
                    return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
                },
            },
        }
    };
    return Controller;
});